<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form ref="queryForm" :model="queryForm" label-width="100px" :inline="true" border>
        <el-form-item label="用户姓名：" prop="userName">
          <el-input
            v-model="queryForm.userName"
            placeholder="请输入用户姓名"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item label="机构名称：" prop="orgName">
          <el-input
            v-model="queryForm.orgName"
            placeholder="请输入机构名称"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item label="操作类型：" prop="operationType">
          <el-select v-model="queryForm.operationType" clearable placeholder="请选择操作类型">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="btnPermission('log:query:list')"
            type="success"
            size="mini"
            icon="el-icon-search"
            plain
            @click="searchData"
          >查询
          </el-button>
          <el-button
            v-if="btnPermission('log:query:list')"
            type="warning"
            size="mini"
            icon="el-icon-refresh-left"
            plain
            @click="resetQueryFrom"
          >重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        stripe
        row-key="id"
        border
        :header-cell-style="{textAlign: 'center'}"
      >
        <el-table-column type="expand" width="60">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="用户姓名">
                <span>{{ props.row.userName }}</span>
              </el-form-item>
              <el-form-item label="所属机构">
                <span>{{ props.row.orgName }}</span>
              </el-form-item>
              <el-form-item label="使用系统">
                <span>{{ props.row.system }}</span>
              </el-form-item>
              <el-form-item label="使用浏览器">
                <span>{{ props.row.browser }}</span>
              </el-form-item>
              <el-form-item label="IP地址">
                <span>{{ props.row.ip }}</span>
              </el-form-item>
              <el-form-item label="操作类型">
                <span>{{ OperationType.getOperationType(props.row.operationType) }}</span>
              </el-form-item>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="请求路径">
                    <span>{{ props.row.requestUrl }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="请求参数">
                    <el-input
                      type="textarea"
                      :rows="4"
                      style="width: 1100px;margin-bottom: 5px;"
                      :value="props.row.params"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="23">
                  <el-form-item label="返回值">
                    <el-input
                      type="textarea"
                      :rows="4"
                      style="width: 1100px;margin-bottom: 5px;"
                      :value="props.row.result"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="1">
                  <el-form-item label="">
                    <i class="el-icon-warning" @click="downLoadTxt(props.row.result,props.row.id)"></i>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="记录旧值">
                <el-input
                  type="textarea"
                  :rows="4"
                  style="width: 550px;"
                  :value="props.row.oldVaue"
                />
              </el-form-item>
              <el-form-item label="记录新值">
                <el-input
                  type="textarea"
                  :rows="4"
                  style="width: 550px;"
                  :value="props.row.newValue"
                />
              </el-form-item>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="行为描述">
                    <span>{{ props.row.remark }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="60"
          :index="indexMethod"
          align="center"
        />
        <el-table-column
          prop="userName"
          label="用户姓名"
        />
        <el-table-column
          prop="orgName"
          label="所在机构"
        />
        <el-table-column
          prop="requestUrl"
          label="请求地址"
          width="500"
        />
        <!--        <el-table-column-->
        <!--          prop="system"-->
        <!--          label="系统版本"-->
        <!--        />-->
        <!--        <el-table-column-->
        <!--          prop="browser"-->
        <!--          label="使用浏览器"-->
        <!--        />-->
        <el-table-column
          prop="ip"
          label="IP地址"
        />
        <el-table-column
          prop="createTime"
          label="访问时间"
          width="160"
        >
          <template slot-scope="scope">
            <span>{{ $moment(scope.row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="operationType"
          label="操作类型"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.operationType === OperationType.DeleteEntity"
              :type="'warning'"
              disable-transitions
            >
              {{ OperationType.getOperationType(scope.row.operationType) }}
            </el-tag>
            <el-tag
              v-else-if="scope.row.operationType === OperationType.RemoveEntity"
              :type="'danger'"
              disable-transitions
            >
              {{ OperationType.getOperationType(scope.row.operationType) }}
            </el-tag>
            <el-tag
              v-else-if="scope.row.operationType ===OperationType.AddEntity"
              :type="'primary'"
              disable-transitions
            >
              {{ OperationType.getOperationType(scope.row.operationType) }}
            </el-tag>
            <el-tag
              v-else-if="scope.row.operationType === OperationType.EditEntity"
              :type="'success'"
              disable-transitions
            >
              {{ OperationType.getOperationType(scope.row.operationType) }}
            </el-tag>
            <el-tag v-else :type="'info'" disable-transitions>
              {{ OperationType.getOperationType(scope.row.operationType) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :current-page="queryCondition.PageIndex"
          :page-sizes="[10,15, 20,40,60,80,100]"
          :page-size="queryCondition.PageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="queryCondition.TotalCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import BasePage from '@/components/BasePage/BasePage'
import {generateQueryItem} from '@/utils/query-item'

export default {
  name: 'LogManagement',
  extends: BasePage,
  data() {
    return {
      queryForm: {
        orgName: null,
        userName: null,
        operationType: null
      },
      options: null
    }
  },
  watch: {},
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    this.url.queryList = 'LogManagement/Post'
  },
  mounted() {
    this.options = this.OperationType.getOpTyKyVa()
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    searchData: function () {
      this.queryCondition.PageIndex = 0
      const array = []
      if (this.queryForm.userName) {
        array.push(generateQueryItem('UserName', this.DataType.String, this.QueryMethod.Like, this.queryForm.userName))
      }
      if (this.queryForm.orgName) {
        array.push(generateQueryItem('OrgName', this.DataType.String, this.QueryMethod.Like, this.queryForm.orgName))
      }
      if (this.queryForm.operationType) {
        array.push(generateQueryItem('OperationType', this.DataType.Int, this.QueryMethod.Equal, this.queryForm.operationType))
      }
      this.queryCondition.QueryItems = array
      this.initData()
    },
    downLoadTxt: function (result, id) {
      if (result && result.toString().endsWith('...')) {
        this.exportFile('LogManagement/DownloadMonitoringLog/' + id, null, id + '.txt')
      } else {
        this.$message.warning('无可下载附件')
      }
    }
  }
}
</script>

<style scoped>
.pagination {
  text-align: right;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
